package controllers.games;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JTextField;

import models.games.GocceDiParole;
import models.games.GocceDiParole.VerifyResult;
import views.games.GocceDiParoleView;
import core.GameLauncher;
import core.GameLauncher.State;

public class GocceDiParoleController extends KeyAdapter implements ActionListener {

	private GocceDiParole model;
	private GocceDiParoleView view;
	private boolean aggiorna;
	private GameLauncher gameLauncher;

	public GocceDiParoleController(GocceDiParole model, GocceDiParoleView view,
			GameLauncher gameLauncher) {
		this.model = model;
		this.view = view;
		this.gameLauncher = gameLauncher;
		view.addActionListenerToGame(this);
		view.addKeyListenerToGame(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JButton) {
			JButton source = (JButton) e.getSource();
			String inputText = source.getText();
			if (inputText.equals("Ricomincia")) {
				GocceDiParole newModel = null;
				try {
					newModel = new GocceDiParole(model.getCurrentUser());
				} catch (SQLException | IOException e1) {
					System.out.println(e
							+ " in GocceDiParoleController -> actionPerformed");
					e1.printStackTrace();
				}
				GocceDiParoleView newView = new GocceDiParoleView(
						newModel.getFirstString());
				GocceDiParoleController newController = new GocceDiParoleController(
						newModel, newView, gameLauncher);
				gameLauncher.setGame(newModel, newView, newController);
				gameLauncher.startTransition(State.GAME);
				model.addGameToDatabase();
				System.out.println("Ricomincia");
			}

			if (inputText.equals("Menu Giochi")) {
				gameLauncher.startTransition(State.GAME_MENU);
				model.addGameToDatabase();
				System.out.println("Menu Giochi");
			}
		} else {
			JTextField input = (JTextField) e.getSource();
			String word = model.getFirstString() + input.getText();
			word.trim();

			int letters = 0;
			try {
				System.out.println("INSERITA: " + word);
				VerifyResult vr = model.verify(word);
				if (vr.equals(VerifyResult.CORRETTO)) {
					System.out.println("VERIFICATO");
					letters = word.length();
					view.incrementCountWord(letters);
					view.incrementRow(word.length() - 4);
					view.setGameScore(model.getScore(), model.getCountWord());
					view.changeOvalPosition(0);
					gameLauncher.correctAnswer();

				} else if (vr.equals(VerifyResult.ERRATO)) {
					view.showError();
					System.out.println("NO");
				} else {
					view.showRipetition();
					System.out.println("GIA' INSERITO");
				}
				if (view.getUpdateStatus()) {
					model.semplifyGame();
					view.refresh(model.getFirstString());
					view.updated();
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
			int ovalPosition = view.getCurrentOvalPosition();
			if (ovalPosition > 0)
				view.changeOvalPosition(view.getCurrentOvalPosition() - 1);
			view.removeError();
		} else {
			// MANCA GESTIONI INPUT ERRATI (NUMERI, ALTRI TASTI)
			JTextField source = (JTextField) e.getSource();
			switch (source.getText().length()) {
			case 0:
				break;
			case 1:
				view.changeOvalPosition(0);
				break;
			case 2:
				view.changeOvalPosition(1);
				break;
			case 3:
				view.changeOvalPosition(2);
				break;
			case 4:
				view.changeOvalPosition(3);
				break;
			case 5:
				view.changeOvalPosition(4);
				break;
			case 6:
				view.changeOvalPosition(5);
				break;
			case 7:
				view.changeOvalPosition(6);
				break;
			case 8:
				view.changeOvalPosition(7);
				break;
			case 9:
				view.changeOvalPosition(8);
				break;
			default:
				view.changeOvalPosition(9);
				break;
			}
		}

	}
}
